import '../AI/tina_dart/BaseAPI.dart';
import '../AI/dify_datasets.dart';
import '../serve/file_serve.dart';
import '../AI/screen_shot_serve.dart';
import '../AI/smart_floating_ball_service.dart';

/// 简单的服务定位器
/// 统一管理所有服务的依赖注入
class ServiceLocator {
  static final ServiceLocator _instance = ServiceLocator._internal();
  
  // 服务实例
  FileServe? _fileServe;
  BaseAPI? _llm;
  DifyKnowledgeDatabase? _difyKnowledge;
  ScreenShotServe? _screenShotServe;
  
  ServiceLocator._internal();
  
  static ServiceLocator get instance => _instance;
  
  /// 初始化所有服务
  static Future<void> initializeServices() async {
    final locator = _instance;
    
    // 文件服务（最基础的服务）
    locator._fileServe = FileServe();
    await locator._fileServe!.initialize();
    
    // LLM服务
    final llmConfig = await locator._fileServe!.getConfig('LLM');
    locator._llm = BaseAPI(
      model: llmConfig['model'] ?? 'qwen-plus',
      apiKey: llmConfig['api_key'] ?? 'sk-aa328698ca6f4a7c9c0dde0b9851a772',
      baseUrl: llmConfig['base_url'] ?? 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions',
    );
    
    // Dify知识库服务
    locator._difyKnowledge = await DifyKnowledgeDatabase.create();
    
    // 截图服务
    locator._screenShotServe = ScreenShotServe(locator._fileServe!);
    await locator._screenShotServe!.initialize(locator._llm!);
    
    // 智能悬浮球服务
    await SmartFloatingBallService.setScreenShotServe(locator._screenShotServe!);
    await SmartFloatingBallService.initialize();
  }
  
  /// 获取文件服务
  static FileServe get fileServe => _instance._fileServe!;
  
  /// 获取LLM服务
  static BaseAPI get llm => _instance._llm!;
  
  /// 获取Dify知识库服务  
  static DifyKnowledgeDatabase get difyKnowledge => _instance._difyKnowledge!;
  
  /// 获取截图服务
  static ScreenShotServe get screenShotServe => _instance._screenShotServe!;
  
  /// 检查服务是否已初始化
  static bool get isInitialized => 
    _instance._fileServe != null && 
    _instance._llm != null && 
    _instance._difyKnowledge != null &&
    _instance._screenShotServe != null;
} 